package com.contactmgr.activity;

import java.util.List;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;

import com.contactmgr.R;
import com.contactmgr.listadapter.CallListAdapter;
import com.contactmgr.listadapter.ContactListAdapter;
import com.contactmgr.listadapter.SeparatedListAdapter;
import com.contactmgr.listadapter.SmsListAdapter;
import com.contactmgr.logObjects.CallDataLog;
import com.contactmgr.logObjects.Contact;
import com.contactmgr.logObjects.Sms;
import com.contactmgr.utils.CallLogReader;
import com.contactmgr.utils.ContactNameResolver;
import com.contactmgr.utils.ContactReader;
import com.contactmgr.utils.SmsReader;

public class DisplayLogActivityNotWorking extends TabActivity {
	List<Contact> contactList;

	List<CallDataLog> nameResolvedCallData;
	private TabHost tabHost;
	private ListView callLogListView;
	private ListView smsLogListView;
	private ListView contactListView;
	List<Sms> inboxResolved;
	List<Sms> sentItemsResolved;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainlayoutnotworking);
		initComponents();
		readLogs();
//		showDatasInTabs();
		showDatasInTabsWithSeparator();
	}

	private void initComponents() {
		tabHost = getTabHost();
		callLogListView = (ListView) findViewById(R.id.callListV);
		smsLogListView = (ListView) findViewById(R.id.smsListV);
		contactListView = (ListView) findViewById(R.id.contactListV);
	}

	private void readLogs() {
		contactList = ContactReader.getContacts(getContentResolver());
		for (Contact contact : contactList) {
			System.out.println(contact.toString());
		}
		List<CallDataLog> callLog = CallLogReader.getAllCallLogs(getContentResolver());
		nameResolvedCallData = ContactNameResolver.resolveNamesInCallLog(contactList, callLog);
		for (CallDataLog callDataLog : nameResolvedCallData) {
			System.out.println(callDataLog.toString());
		}
		List<Sms> inbox = SmsReader.readInbox(getContentResolver());
		List<Sms> sentItems = SmsReader.readSentItems(getContentResolver());
		inboxResolved = ContactNameResolver.resolveNamesInSms(contactList, inbox);
		sentItemsResolved = ContactNameResolver.resolveNamesInSms(contactList, sentItems);
	}

	private void showDatasInTabs() {

		smsLogListView.setAdapter(new SmsListAdapter(this, R.layout.list_sms, inboxResolved));
		callLogListView.setAdapter(new CallListAdapter(this, R.layout.list_call, nameResolvedCallData));
		contactListView.setAdapter(new ContactListAdapter(this, R.layout.list_contact, contactList));
		
		// add views to tab host
		tabHost.addTab(tabHost.newTabSpec("Contacts").setIndicator("Contacts").setContent(new TabContentFactory() {
			public View createTabContent(String arg0) {
				return contactListView;
			}
		}));

		tabHost.addTab(tabHost.newTabSpec("CallLog").setIndicator("CallLog").setContent(new TabContentFactory() {
			public View createTabContent(String arg0) {
				return callLogListView;
			}
		}));
		tabHost.addTab(tabHost.newTabSpec("SMS").setIndicator("SMS").setContent(new TabContentFactory() {
			public View createTabContent(String arg0) {
				return smsLogListView;
			}
		}));
		tabHost.setCurrentTab(0);
	}
	private void showDatasInTabsWithSeparator() {

		/**separated list adapter for sms**/
		SeparatedListAdapter smsAllAdp = new SeparatedListAdapter(this);
		ArrayAdapter<Sms> smsInboxAdp=new SmsListAdapter(this, R.layout.list_sms, inboxResolved);
		ArrayAdapter<Sms> smsSentAdp=new SmsListAdapter(this, R.layout.list_sms, sentItemsResolved);
		smsAllAdp.addSection("Inbox", smsInboxAdp);
		smsAllAdp.addSection("SentItems", smsSentAdp);
		smsLogListView.setAdapter(smsAllAdp);
		
		
		/**separated list adapter for call log**/
		SeparatedListAdapter callLogAllAdp = new SeparatedListAdapter(this);
		ArrayAdapter<CallDataLog> missedCallAdp = new CallListAdapter(this, R.layout.list_call, nameResolvedCallData);
		ArrayAdapter<CallDataLog> incomingCallAdp = new CallListAdapter(this, R.layout.list_call, nameResolvedCallData);
		ArrayAdapter<CallDataLog> outgoingCallAdp = new CallListAdapter(this, R.layout.list_call, nameResolvedCallData);
		callLogAllAdp.addSection("Missed Calls", missedCallAdp);
		callLogAllAdp.addSection("Incoming Calls", incomingCallAdp);
		callLogAllAdp.addSection("Outgoing Calls", outgoingCallAdp);
		callLogListView.setAdapter(callLogAllAdp);
		
		//contactview 
		SeparatedListAdapter contactAllAdp = new SeparatedListAdapter(this);
		ArrayAdapter<Contact> contactAdp = new ContactListAdapter(this, R.layout.list_contact, contactList);
		contactAllAdp.addSection("Contacts", contactAdp);
		contactListView.setAdapter(contactAllAdp);
		
		// add views to tab host
		
		tabHost.addTab(tabHost.newTabSpec("Contacts").setIndicator("Contacts").setContent(new TabContentFactory() {
			public View createTabContent(String arg0) {
				return contactListView;
			}
		}));

		tabHost.addTab(tabHost.newTabSpec("CallLog").setIndicator("CallLog").setContent(new TabContentFactory() {
			public View createTabContent(String arg0) {
				return callLogListView;
			}
		}));
		tabHost.addTab(tabHost.newTabSpec("SMS").setIndicator("SMS").setContent(new TabContentFactory() {
			public View createTabContent(String arg0) {
				return smsLogListView;
			}
		}));
		tabHost.setCurrentTab(0);
	}
	
}
